﻿using Flurl;
using Flurl.Http;
using Modules.Units;
using Newtonsoft.Json;
using Sorting.App.Models;
using Sorting.App.Models.Enum;
using Sorting.App.Services;

namespace Sorting.App.Services
{
    public class ShelfService
    {

        private ServiceApis serviceApis = new ServiceApis();

        /// <summary>
        /// 获取全部的储位信息  主界面表格使用
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<ShelfJoinWardModel>> GetShelves()
        {

            try
            {
                var res = await serviceApis.GetShelvesApi
               .WithOAuthBearerToken(AppsettingUnits.Token)
               .PostAsync()
               .ReceiveJson<ResfulModel<IEnumerable<ShelfJoinWardModel>>>();

                return res.data;

            }
            catch (Exception ex)
            {

                throw;
            }
      


        }

        /// <summary>
        /// 获取储位信息根据UUId
        /// </summary>
        /// <param name="ShelfUuid"></param>
        /// <returns></returns>
        public async Task<ShelfEntity> GetShelfEntityByUuid(string ShelfUuid)
        {
            var res= await serviceApis.GetShelfEntityByUuidApi
                 .WithOAuthBearerToken( AppsettingUnits.Token)
                   .SetQueryParams(new
                   {
                       uuid = ShelfUuid,
                   }).PostAsync()
                   .ReceiveJson<ResfulModel<ShelfEntity>>();
            return res.data;
        }


       /// <summary>
       /// 删除储位信息根据UUID
       /// </summary>
       /// <param name="Id"></param>
       /// <returns></returns>
        public async Task<ResfulModel<bool>> DeleteShelfById(string Id)
        {


            return await serviceApis.DeleteShelfByIdApi
                 .WithOAuthBearerToken( AppsettingUnits.Token)
                .SetQueryParams(new
                {
                    Id = Id,
                }).PostAsync()
                .ReceiveJson<ResfulModel<bool>>();
        }




        /// <summary>
        /// 新增储位信息
        /// </summary>
        /// <param name="shelfEntity"></param>
        /// <returns></returns>
        public async Task<ResfulModel<bool>> AddShelf(ShelfEntity shelfEntity)
        {


            return await serviceApis.AddShelfApi
                 .WithOAuthBearerToken( AppsettingUnits.Token)
                    .PostJsonAsync(shelfEntity)
                    .ReceiveJson<ResfulModel<bool>>();
        }






        /// <summary>
        /// 修改储位信息
        /// </summary>
        /// <param name="shelfEntity"></param>
        /// <returns></returns>
        public async Task<ResfulModel<bool>> EditShelf(ShelfEntity shelfEntity)
        {

            var res =  await serviceApis.EditShelfApi
                 .WithOAuthBearerToken( AppsettingUnits.Token)
                    .PostJsonAsync(shelfEntity)
                    .ReceiveJson<ResfulModel<bool>>();

            return res;

        }



        /// <summary>
        /// 获取储位信息根据病区编码
        /// </summary>
        /// <param name="wardcode"></param>
        /// <returns></returns>
        /// 
        public ShelfEntity GetShelfByWardNo(string wardcode)
        {

            var res = serviceApis.GetShelfByWardNoApi
                 .WithOAuthBearerToken(AppsettingUnits.Token)
                .SetQueryParams(new
                {
                    wardcode = wardcode,
                }).PostAsync()
                .ReceiveJson<ResfulModel<ShelfEntity>>();

            return res.Result.data;

        }

        /// <summary>
        /// 获取储位信息根据病区编码
        /// </summary>
        /// <param name="wardcode"></param>
        /// <returns></returns>
        /// 
        public List< ShelfEntity> GetShelfListByWardNo(string wardcode)
        {

            var res = serviceApis.GetShelfListByWardNoApi
                 .WithOAuthBearerToken(AppsettingUnits.Token)
                .SetQueryParams(new
                {
                    wardcode = wardcode,
                }).PostAsync()
                .ReceiveJson<ResfulModel<List<ShelfEntity>>>();

            return res.Result.data;

        }
        //public async Task<ShelfEntity> GetShelfByWardNo(string wardcode)
        //{

        //    var res= await serviceApis.GetShelfByWardNoApi
        //         .WithOAuthBearerToken( AppsettingUnits.Token)
        //        .SetQueryParams(new
        //        {
        //            wardcode = wardcode,
        //        }).PostAsync()
        //        .ReceiveJson<ResfulModel<ShelfEntity>>();

        //    return res.data;

        //}



        /// <summary>
        /// 获取储位信息根据储位编码和模组
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="group"></param>
        /// <returns></returns>
        public async Task<ShelfEntity> GetShelfEntityByCode(object Code)
        {

            var res = await serviceApis.GetShelfEntityByCodeApi
                 .WithOAuthBearerToken( AppsettingUnits.Token)
             .SetQueryParams(new
             {
                 Code = Code,
             }).PostAsync()
             .ReceiveJson<ResfulModel<ShelfEntity>>();
            return res.data;

        }


        /// <summary>
        /// 获取储位信息  用于下拉
        /// </summary>
        /// <returns></returns>

        public async Task<IEnumerable<ShelfDataModel>> GetShelfToDatas()
        {

            var res= await serviceApis.GetShelfToDatasApi
                 .WithOAuthBearerToken( AppsettingUnits.Token)
                .PostAsync()
                .ReceiveJson<ResfulModel<IEnumerable<ShelfDataModel>>>();


            return res.data;
        }


        /// <summary>
        /// 清除库存数量和状态
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResfulModel<bool>> ClearShelfStore()
        {
            return await serviceApis.ClearShelfStoreApi
                 .WithOAuthBearerToken( AppsettingUnits.Token)
            .PostAsync()
            .ReceiveJson<ResfulModel<bool>>();
        }

        /// <summary>
        /// 更新库存
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResfulModel<bool>> UpdateShelfStore(ShelfEntity shelfDto)
        {
            return await serviceApis.UpdateShelfStoreApi
                 .WithOAuthBearerToken(AppsettingUnits.Token)
            .PostJsonAsync(shelfDto)
            .ReceiveJson<ResfulModel<bool>>();
        }

        
    }

}
